package com.ihr360.excel.model.dto;

import com.ihr360.commons.model.entity.StringIdWithTimeAndCompanyId;
import com.ihr360.excel.logs.ExcelLogType;
import com.ihr360.excel.model.type.JournalType;
import com.ihr360.hibernate.type.json.JsonStringType;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.util.Date;

@TypeDefs({
        @TypeDef(name = "json", typeClass = JsonStringType.class)
})
@Entity
@DynamicUpdate
public class SpreadsheetJournal extends StringIdWithTimeAndCompanyId {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sheet_id")
    private SpreadSheet sheet;

    @Column(nullable = false)
    @Enumerated(value = EnumType.STRING)
    private JournalType type;

    @Column(nullable = false)
    @Enumerated(value = EnumType.STRING)
    private ExcelLogType logType;

    @Column(length = 100)
    private String logMsgKey;

    @Column(length = 100)
    private String defaultLogMsg;

    @Column(columnDefinition = "text")
    @Type(type = "json")
    private Object[] args;

    @Column(length = 10)
    private Integer rowNum;

    @Column(length = 4)
    private Integer colNum;

    /**
     * 处理进度唯一标识
     */
    @Column(length = 200)
    private String progressKey;

    @Column
    private Boolean isDeleted = false;

    public SpreadSheet getSheet() {
        return sheet;
    }

    public void setSheet(SpreadSheet sheet) {
        this.sheet = sheet;
    }

    public JournalType getType() {
        return type;
    }

    public void setType(JournalType type) {
        this.type = type;
    }

    public ExcelLogType getLogType() {
        return logType;
    }

    public void setLogType(ExcelLogType logType) {
        this.logType = logType;
    }

    public String getLogMsgKey() {
        return logMsgKey;
    }

    public void setLogMsgKey(String logMsgKey) {
        this.logMsgKey = logMsgKey;
    }

    public String getDefaultLogMsg() {
        return defaultLogMsg;
    }

    public void setDefaultLogMsg(String defaultLogMsg) {
        this.defaultLogMsg = defaultLogMsg;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public Integer getRowNum() {
        return rowNum;
    }

    public void setRowNum(Integer rowNum) {
        this.rowNum = rowNum;
    }

    public Integer getColNum() {
        return colNum;
    }

    public void setColNum(Integer colNum) {
        this.colNum = colNum;
    }

    public String getProgressKey() {
        return progressKey;
    }

    public void setProgressKey(String progressKey) {
        this.progressKey = progressKey;
    }

    public Boolean getDeleted() {
        return isDeleted;
    }

    public void setDeleted(Boolean deleted) {
        isDeleted = deleted;
    }


    public static SpreadsheetJournal getJournal(JournalType journalType, String logMsgKey, String defaultLogMsg, Object[] args, String progressKey) {
        SpreadsheetJournal spreadsheetJournal = new SpreadsheetJournal();
        spreadsheetJournal.setType(journalType);
        spreadsheetJournal.setLogMsgKey(logMsgKey);
        spreadsheetJournal.setDefaultLogMsg(defaultLogMsg);
        spreadsheetJournal.setArgs(args);
        spreadsheetJournal.setProgressKey(progressKey);
        spreadsheetJournal.setCreatedAt(new Date());
        spreadsheetJournal.setUpdatedAt(new Date());
        return spreadsheetJournal;
    }

}
